package com.atguigu.tingshu.user.api;


import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.user.*;
import com.atguigu.tingshu.user.service.UserCollectService;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserSubscribeService;
import com.atguigu.tingshu.vo.user.UserCollectVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.atguigu.tingshu.vo.user.UserSubscribeVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// api/user/userInfo/getUserInfoById/{userId}}
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserSubscribeService userSubscribeService;
    @Autowired
    private UserCollectService userCollectService;





    // 分页查询用户的收藏的声音列表 并且按照时间降序排序
    // http://127.0.0.1:8500/api/user/userInfo/findUserCollectPage/1/10
    @GuiguLogin
    @Operation(summary = "分页查询用户的收藏的声音列表")
    @GetMapping("findUserCollectPage/{pageNum}/{pageSize}")
    public Result<Page<UserCollectVo>> findUserCollectPage(@PathVariable Long pageNum,
                                                    @PathVariable Long pageSize) {
       Page<UserCollectVo> page =  userCollectService.findUserCollectPage(pageNum, pageSize);
        return Result.ok(page);
    }

    // 分页查询用户的收藏的声音列表 并且按照时间降序排序
    // http://127.0.0.1:8500/api/user/userInfo/findUserCollectPage/1/10
    @GuiguLogin
    @Operation(summary = "分页查询用户的订阅的声音列表")
    @GetMapping("findUserSubscribePage/{pageNum}/{pageSize}")
    public Result<Page<UserSubscribeVo>> findUserSubscribePage(@PathVariable Long pageNum,
                                                               @PathVariable Long pageSize) {
        Page<UserSubscribeVo> page =  userSubscribeService.findUserSubscribePage(pageNum, pageSize);
        return Result.ok(page);
    }


    // 远程调用 获取用户的信息
    @Operation(summary = "远程调用根据用户id获取用户的信息")
    @GetMapping("getUserInfoById/{userId}")
    public Result<UserInfoVo> getUserInfoById(@PathVariable("userId") Long userId) {
        return Result.ok(userInfoService.getUserInfoById(userId));
    }

    // 远程调用 获取用户已经购买的声音id列表
    // api/user/userInfo/getUserBuyTrackIdList/{userId}/{albumId}
    @Operation(summary = "远程调用根据用户id获取用户已经购买某专辑的的声音id列表")
    @GetMapping("getUserBuyTrackIdList/{userId}/{albumId}")
    public Result<List<UserPaidTrack>> getUserBuyTrackIdList(@PathVariable("userId") Long userId,
                                                             @PathVariable("albumId") Long albumId) {
        return Result.ok(userInfoService.getUserBuyTrackIdList(userId, albumId));
    }

    // todo 远程调用获取 用户是否购买过该专辑
    // api/user/userInfo/isBuyAlbum
    @Operation(summary = "远程调用根据用户id和albumId判断用户是否购过买该专辑")
    @GetMapping("isBuyAlbum/{userId}/{albumId}")
    public Result<Boolean> isBuyAlbum(@PathVariable("userId") Long userId,
                                      @PathVariable("albumId") Long albumId) {
        Boolean result = userInfoService.isBuyAlbum(userId, albumId);

        return Result.ok(result);
    }

    // 判断用户是否订阅某专辑
    // http://127.0.0.1:8500/api/user/userInfo/isSubscribe/946
    @GuiguLogin(required = false)
    @Operation(summary = "判断用户是否订阅某专辑")
    @GetMapping("isSubscribe/{albumId}")
    public Result isSubscribe(@PathVariable("albumId") Long albumId) {

        long count = userSubscribeService.count(new LambdaUpdateWrapper<UserSubscribe>().
                eq(UserSubscribe::getUserId,
                        AuthContextHolder.getUserId()).eq(UserSubscribe::getAlbumId, albumId));
        return Result.ok(count > 0);
    }

    // http://127.0.0.1:8500/api/user/userInfo/isCollect/51946
    // 判断用户是否订阅某声音
    @GuiguLogin(required = false)
    @Operation(summary = "判断用户是否收藏某声音")
    @GetMapping("isCollect/{trackId}")
    public Result isCollect(@PathVariable("trackId") Long trackId) {

        if (AuthContextHolder.getUserId() == null) {
            return Result.ok(false);
        }
        long count = userCollectService.count(new LambdaUpdateWrapper<UserCollect>()
                .eq(UserCollect::getUserId, AuthContextHolder.getUserId())
                .eq(UserCollect::getTrackId, trackId));
        return Result.ok(count > 0);
    }
    // 远程调用user服务 保存交易记录 saveUserPaidRecord
    // api/user/userInfo/saveUserPaidRecord
    @Operation(summary = "远程调用user服务 保存交易记录")
    @PostMapping("saveUserPaidRecord")
    public Result saveUserPaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        userInfoService.saveUserPaidRecord(userPaidRecordVo);
        return Result.ok();
    }

}

